﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Runtime.Common.Interface.GameWorld.Core.Interface;
using Runtime.Common.Utility;
using Runtime.Data.Interface;
using UnityEngine;

namespace Runtime.Core.GameBehaviour
{
    public class BehaviourFactory : IInit
    {
        ConcurrentDictionary<string, Type> dict = new ConcurrentDictionary<string, Type>();
        BehaviourHelper behaviourHelper;

        public void Init()
        {
            behaviourHelper = new GameObject("BehaviourHelper").AddComponent<BehaviourHelper>();
            List<Type> tyList = AssemblyEx.GetTypesByInheritObjectType<IGameBehaviourObject>(true);
            foreach (var item in tyList)
            {
                dict.TryAdd(item.Name, item);
            }
        }

        public T Create<T>(IFunctionParameter gameData) where T : IGameBehaviourObject
        {
            Type t = typeof(T);
            if (t.GetInterface("") == null)
            {
                return default;
            }

            T obj = (T)Activator.CreateInstance(t, gameData);
            behaviourHelper.Add(obj);
            return obj;
        }
    }
}